package com.uchengwang.ui.cm_ui.umsg.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.uchengwang.R;
import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.http.listener.OnHttpResultSingleListener;
import com.uchengwang.functions.http.listener.OnResultListener;
import com.uchengwang.functions.http.manager.OSSManager;
import com.uchengwang.functions.image.ImageManager;
import com.uchengwang.ui.base_ui.bean.MoreBean;
import com.uchengwang.ui.base_ui.ui.BaseWithTitleActivity;
import com.uchengwang.ui.cm_ui.umsg.bean.WantChatBean;
import com.uchengwang.ui.cm_ui.umsg.http.ChatHttpManager;
import com.uchengwang.ui.utils.PopupUtils;
import com.uchengwang.ui.utils.UCommonUtil;

import java.util.ArrayList;
import java.util.List;

import io.rong.imlib.model.Conversation;

/**
 * Created by U城网 on 2017/12/15.
 * 享聊别人的主页
 */

public class WantChatPersonInfoActivity extends BaseWithTitleActivity implements View.OnClickListener{
    private ImageView head_bigImg, head_1, head_2;
    private RelativeLayout name_rl, level_rl;
    private TextView name, levelTv, schoolTv, signTv;
    private ImageView level_img;
    private LinearLayout sign_rl;

    private Button chat_bt;
    private long uid;
    private WantChatBean bean;
    private ImageView sexImg;
    private List<String> imgs;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_want_chat_person_info);
        uid = getIntent().getLongExtra(BaseConfig.UID, 0);
        setTitle("资料");
        setRightImage(R.drawable.more_operate, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //举报别人
                final List<MoreBean> beanList = new ArrayList<MoreBean>();
                beanList.add(new MoreBean(R.drawable.selector_report, "举报"));
                PopupUtils.getUtils().bottomMore(WantChatPersonInfoActivity.this, titleBar, beanList, new PopupUtils.OnItemPopupClickListener() {
                    @Override
                    public void onItemClick(int index, String title) {
                        PopupUtils.getUtils().dismiss();
                        switch (index){
                            case 0://举报
                                ChatHttpManager.getManager().report(WantChatPersonInfoActivity.this, "举报", uid, new OnResultListener() {
                                    @Override
                                    public void onResult(int stutas, String msg, int total, String data) {
                                        if (stutas ==0){
                                            UCommonUtil.getUtil().showToast(WantChatPersonInfoActivity.this, "举报成功");
                                        }else {
                                            UCommonUtil.getUtil().showToast(WantChatPersonInfoActivity.this, "举报失败");
                                        }
                                    }
                                });
                                break;
                        }
                    }
                });
            }
        });
        initView();

    }

    private void initView() {
        head_bigImg = (ImageView) findViewById(R.id.wantMe_head_bigImg);//三个头像
        head_1 = (ImageView) findViewById(R.id.wantMe_head_1);
        head_2 = (ImageView) findViewById(R.id.wantMe_head_2);

        name_rl = (RelativeLayout) findViewById(R.id.wantMe_name_rl);
        name = (TextView) findViewById(R.id.wantMe_name);
        sexImg = (ImageView) findViewById(R.id.wantMe_sex);

        level_rl = (RelativeLayout) findViewById(R.id.wantMe_level_rl);//魅力值显示
        levelTv = (TextView) findViewById(R.id.wantMe_level);
        level_img = (ImageView) findViewById(R.id.wantMe_level_img);

        schoolTv = (TextView) findViewById(R.id.wantMe_school);
        sign_rl = (LinearLayout) findViewById(R.id.wantMe_sign_rl);
        signTv = (TextView) findViewById(R.id.wantMe_sign);
        chat_bt = (Button) findViewById(R.id.wantMe_chat_bt);
        chat_bt.setOnClickListener(this);

        ImageManager.getInstance().setRoundSize(5).loadImage(this, head_bigImg, R.drawable.icon_want_chat_default_img, ImageManager.ImageType.ROUND);
        ImageManager.getInstance().setRoundSize(5).loadImage(this, head_1, R.drawable.icon_want_chat_default_img, ImageManager.ImageType.ROUND);
        ImageManager.getInstance().setRoundSize(5).loadImage(this, head_2, R.drawable.icon_want_chat_default_img, ImageManager.ImageType.ROUND);

        getInfo();

    }


    private void initData(){
        if (bean != null){
            if (uid == UCommonUtil.getUtil().getId()){
                chat_bt.setVisibility(View.GONE);
            }
            name.setText(bean.getChatName());
            int sex = bean.getSex();//0=女 1=男
            if (sex ==0){
                ImageManager.getInstance().loadImage(this, sexImg, R.drawable.icon_want_chat_woman, ImageManager.ImageType.NARMOL);
            }else {
                ImageManager.getInstance().loadImage(this, sexImg, R.drawable.icon_want_chat_man, ImageManager.ImageType.NARMOL);
            }
            schoolTv.setText(bean.getUniName());
            signTv.setText(bean.getChatSign());

            int examineType = bean.getIsExamine();
            switch (examineType){//-1=未通过 0=待审核 1=通过
                case -1:
                    levelTv.setText("未通过");
                    break;
                case 0:
                    levelTv.setText("审核中");
                    break;
                case 1:
                    levelTv.setText("1");
                    level_img.setVisibility(View.VISIBLE);
                    break;
            }

            imgs = bean.getHeads();
            if (imgs != null && imgs.size() >0){
                for (int i=0; i<imgs.size(); i++){
                    String url = imgs.get(i);
                    switch (i){
                        case 0:
                            if (!TextUtils.isEmpty(url)){
                                ImageManager.getInstance().setRoundSize(5).loadImage(this, head_bigImg, url, ImageManager.ImageType.ROUND);
                                head_bigImg.setOnClickListener(this);
                            }else {
                                ImageManager.getInstance().setRoundSize(5).loadImage(this, head_bigImg, R.drawable.icon_want_chat_default_img, ImageManager.ImageType.ROUND);
                            }
                            break;
                        case 1:
                            if (!TextUtils.isEmpty(url)){
                                ImageManager.getInstance().setRoundSize(5).loadImage(this, head_1, url, ImageManager.ImageType.ROUND);
                                head_1.setOnClickListener(this);
                            }else {
                                ImageManager.getInstance().setRoundSize(5).loadImage(this, head_1, R.drawable.icon_want_chat_default_img, ImageManager.ImageType.ROUND);
                            }
                            break;
                        case 2:
                            if (!TextUtils.isEmpty(url)){
                                ImageManager.getInstance().setRoundSize(5).loadImage(this, head_2, url, ImageManager.ImageType.ROUND);
                                head_2.setOnClickListener(this);
                            }else {
                                ImageManager.getInstance().setRoundSize(5).loadImage(this, head_2, R.drawable.icon_want_chat_default_img, ImageManager.ImageType.ROUND);
                            }
                            break;
                    }
                }
            }

            if (bean.isChat()){//之前聊过
                chat_bt.setBackgroundResource(R.drawable.selector_get_code);
                chat_bt.setText("继续聊");
            }else {
                chat_bt.setBackgroundResource(R.drawable.selector_fast_release);
                chat_bt.setText("聊聊呗");
            }


        }
    }

    /**
     * 获取某一个人的享聊信息
     * */
    private void getInfo(){
        ChatHttpManager.getManager().getChatOne(this, 1, uid, new OnHttpResultSingleListener<WantChatBean>() {
            @Override
            public void onResult(int status, String msg, WantChatBean wantChatBean) {
                if (wantChatBean != null){
                    bean = wantChatBean;
                    initData();
                }
            }
        });
    }




    @Override
    public void onClick(View view) {
        Intent intent = null;
        switch (view.getId()){
            case R.id.wantMe_head_bigImg:
                UCommonUtil.getUtil().startImagePagerActivity(this, OSSManager.OSS_SHARE, bean.getHeads(), 0, false);
                break;
            case R.id.wantMe_head_1:
                UCommonUtil.getUtil().startImagePagerActivity(this, OSSManager.OSS_SHARE, bean.getHeads(), 1, false);
                break;
            case R.id.wantMe_head_2:
                UCommonUtil.getUtil().startImagePagerActivity(this, OSSManager.OSS_SHARE, bean.getHeads(), 2, false);
                break;
            case R.id.wantMe_chat_bt://聊天
                UCommonUtil.getUtil().toWantChat(this, bean, Conversation.ConversationType.PRIVATE);
                break;
        }

        if (intent != null){
            startActivity(intent);
        }
    }
}
